package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class GoodsBzShowRvAdapter extends RecyclerView.Adapter<GoodsBzShowRvAdapter.GoodsSortBzShowRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> beanList = new ArrayList<>();
    //    private List<GoodsSortSearchAllBzTimeBean.DataBean> timeBeanList = new ArrayList<>();
//    private String state;
    private GoodsBzRvListener goodsBzRvListener;
    private int currentNum = 3;

    public GoodsBzRvListener getGoodsBzRvListener() {
        return goodsBzRvListener;
    }

    public void setGoodsBzRvListener(GoodsBzRvListener goodsBzRvListener) {
        this.goodsBzRvListener = goodsBzRvListener;
    }

    public GoodsBzShowRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
//        this.state = state;
    }

    public void setBeanList(List<GoodsSortSearchAllBzBean.DataBean> beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }

//    public void setSxList(List<GoodsSortSearchAllBzTimeBean.DataBean> timeBeanList) {
//        this.timeBeanList = timeBeanList;
//        notifyDataSetChanged();
//    }

    @NonNull
    @Override
    public GoodsSortBzShowRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.search_result_peixun_item, viewGroup, false);
        return new GoodsSortBzShowRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsSortBzShowRvViewHolder holder, final int i) {
//        if (state.equals("1")){
        String time = beanList.get(i).getPostTime();
        String newTiem = time.substring(0, time.indexOf("T"));
        holder.type.setText(beanList.get(i).getAttrShow());
        holder.no.setText(beanList.get(i).getNo());
        holder.title.setText(beanList.get(i).getTitle());
        holder.fabudata.setText("发布日期:" + newTiem);
        holder.shishidata.setText("实施日期:" + newTiem);
        holder.price.setText("¥" + beanList.get(i).getPrice());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = beanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);

                Intent intent = DetailProductActivity.getIntent(context, split[2]);
                context.startActivity(intent);
            }
        });
        //添加购物车
        holder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = beanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "splite:" + split[2]);
                String skuStr = "{" + "\"" + split[2].toString() + "\"" + ":" + "1}";
                goodsBzRvListener.addBzCar(skuStr);
            }
        });
//        }else {
//            String time = timeBeanList.get(i).getPostTime();
//            String newTiem = time.substring(0, time.indexOf("T"));
//            holder.type.setText(timeBeanList.get(i).getAttrShow());
//            holder.no.setText(timeBeanList.get(i).getNo());
//            holder.title.setText(timeBeanList.get(i).getTitle());
//            holder.fabudata.setText("发布日期:" + newTiem);
//            holder.shishidata.setText("实施日期:" + newTiem);
//            holder.price.setText("¥" + timeBeanList.get(i).getPrice());
//            holder.item.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    String s = timeBeanList.get(i).getUrl();
//                    String[] split = s.split("/");
//                    Log.e("cjx","URL："+split[2]);
//                    DetailProductActivity.getIntent(context,split[2]);
//                    Intent intent = new Intent(context, DetailProductActivity.class);
//                    context.startActivity(intent);
//                }
//            });
//            holder.car.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    String s = timeBeanList.get(i).getUrl();
//                    String[] split = s.split("/");
//                    String skuStr = "{"+"\""+split[2]+"\""+":"+"1}";
//                    Log.e("cjx","splite:"+split[2]);
//                    goodsBzRvListener.addBzSxCar(skuStr);
//                }
//            });
//        }

    }

    @Override
    public int getItemCount() {
//        if (state.equals("1"))
        return beanList.size();
//        else
//            return timeBeanList.size();
    }

    class GoodsSortBzShowRvViewHolder extends RecyclerView.ViewHolder {
        TextView type, no, price, title, fabudata, shishidata;
        ImageView car;

        public GoodsSortBzShowRvViewHolder(@NonNull View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.view_goods_Bz_TypeName);
            no = itemView.findViewById(R.id.view_goods_Bz_No);
            price = itemView.findViewById(R.id.view_goods_Bz_Price);
            title = itemView.findViewById(R.id.view_goods_Bz_Desc);
            fabudata = itemView.findViewById(R.id.view_goods_Bz_FaBuData);
            shishidata = itemView.findViewById(R.id.view_goods_Bz_ShiShiData);
            car = itemView.findViewById(R.id.view_goods_Bz_Car);
        }
    }

    public interface GoodsBzRvListener {
        void addBzCar(String skuStr);
//        void addBzSxCar(String skuStr);
    }
}
